


//
//  ZGradeViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/5/10.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "ZGradeViewController.h"
#import "ZSubGradeViewController.h"

#import "HomeModel.h"
@interface ZGradeViewController ()<ZJScrollPageViewDelegate>
/**
 页面分栏的标题
 */
@property (strong,nonatomic) NSArray<NSString *>*titles;

/**
 横向分页滑动组件
 */
@property (strong,nonatomic) ZJScrollPageView *scrollPageView;

@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)NSMutableArray *titlesArray;

@end

@implementation ZGradeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.navigationItem setItemWithTitle:self.name?:@"" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.dataArray = [NSMutableArray array];
    self.titlesArray = [NSMutableArray array];
    ///请求所有标题
    [self loadData];
    // Do any additional setup after loading the view.
}

///添加
- (void)addTabTitlesWithTitles:(NSArray *)titles{
    
    ZJSegmentStyle *style = [[ZJSegmentStyle alloc] init];
    //显示滚动条
    style.showLine = YES;
    // 颜色渐变
    style.gradualChangeTitleColor = YES;
    style.scrollTitle = YES;
    style.scrollLineColor = NColorFrom16RGB(0x23b7e5);
    style.selectedTitleColor = NColorFrom16RGB(0x23b7e5);
    style.segmentViewBounces = YES;
    style.titleFont = kFont15;
    style.adjustCoverOrLineWidth = YES;
    style.autoAdjustTitlesWidth = YES;
    style.segmentHeight = 45.f;
    self.titles = titles;
    // 初始化
    _scrollPageView = [[ZJScrollPageView alloc] initWithFrame:CGRectMake(0, 2, ScreenWidth, ScreenHeight - 20.0) segmentStyle:style titles:self.titles parentViewController:self delegate:self];
    [self.view addSubview:_scrollPageView];
}

///获取所有的栏目标题
- (void)loadData{
    ///
    NSDictionary *dic = @{@"grade":self.grade?:@""};
    [ZNetworkService getRequestWithBaseUrl:kGradeUrl setParameters:dic success:^(id result) {
        YYLog(@"%@",result);
        NSArray *arr = result[@"result"];
        for (NSDictionary *dic in arr) {
            ///注意赋值方式
            ///这种方式ID 才赋值成功  要注意因为model 里使用了mj的关键字替换方法***********
            HomeModel *model = [HomeModel mj_objectWithKeyValues:dic];
            NSString *title = model.subject;
            [self.titlesArray addObject:title];
            [self.dataArray addObject:model];
        }
//        /根据tab titles 来初始化tab
        [self addTabTitlesWithTitles:[self.titlesArray copy]];
        
        //        [UserModel shareInstance].modelArray = self.dataArray;
        
        //        [[NSNotificationCenter defaultCenter]postNotificationName:@"jj" object:self.dataArray];
    } fail:^(NSError *error) {
        
        YYLog(@"%@",error);
        
    } delegater:nil];
    
}


#pragma mark ZJPhotoScrollViewDelegate
- (NSInteger)numberOfChildViewControllers {
    return self.titles.count;
}

- (UIViewController<ZJScrollPageViewChildVcDelegate> *)childViewController:(UIViewController<ZJScrollPageViewChildVcDelegate> *)reuseViewController forIndex:(NSInteger)index {
    
    HomeModel *model = self.dataArray[index];
//
//    [UserModel shareInstance].Id = model.ID;
    ///两种写法都可以，下面的方法可以方便的传值
    
    //    UIViewController<ZJScrollPageViewChildVcDelegate> *childVc = reuseViewController;
    ZSubGradeViewController *childVc = (ZSubGradeViewController *)reuseViewController;
    if (!childVc) {
        
        childVc = [[ZSubGradeViewController alloc] init];
        //        childVc.Id = model.ID;
        if (self.dataArray.count>0) {
            childVc.subId = model.ID;
        }
        
    }
    return childVc;
}

- (BOOL)shouldAutomaticallyForwardAppearanceMethods{
    return NO;
}


@end
